﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Manager;
using Model;
using System.Web.Script.Serialization;
namespace HealthSystem.API
{
    public partial class healthapi : System.Web.UI.Page
    {
        HW_SchoolManager schoolManager=new HW_SchoolManager ();
        ResultManager resultManager = new ResultManager();
        JavaScriptSerializer jser=new JavaScriptSerializer  ();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckIp(base.Request.UserHostAddress.ToString()) && !base.IsPostBack)
            {
                string str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["method"]))
                {
                    str = base.Request.QueryString["method"].ToString();
                    if (str == "schoollist")
                    {
                        if (!string.IsNullOrEmpty(Request.QueryString["area"]))
                        {
                            string str2 = base.Request.QueryString["area"].ToString();
                            var List = schoolManager.GetAPISchool(str2);
                            APISchool apischool = new APISchool();
                            apischool.schoollist = new List<RowSchool>();
                            RowSchool rowschool = null;
                            foreach (var l in List)
                            {
                                rowschool = new RowSchool();
                                rowschool.SchoolBH = l.SchoollCode;
                                rowschool.SchoolName = l.Name;
                                rowschool.SchoolType = TranslateSchoolType(l.SchoolType);
                                apischool.schoollist.Add(rowschool);
                            }
                            base.Response.Write(jser.Serialize(apischool));
                            base.Response.End();
                        }
                    }
                    else if (str == "studentinfo")
                    {
                        if (!string.IsNullOrEmpty(Request.QueryString["BH"]))
                        {
                            APIStudents entity = new APIStudents();
                            entity.studentinfo = resultManager.GetAPIStudent(Request.QueryString["BH"].ToString());
                            Response.Write(jser.Serialize(entity));
                            Response.End();
                        }
                    }
                    else if (str == "studentresult")
                    {
                        if (!string.IsNullOrEmpty(Request.QueryString["BH"]))
                        {
                            List<Dictionary<string, List<CNResult>>> dics = new List<Dictionary<string, List<CNResult>>>();
                            APIResult apiresult = new APIResult();
                            HW_Result2013 result = null;
                            List<CNResult> list = new List<CNResult>();
                            Dictionary<string, List<CNResult>> dic = new Dictionary<string, List<CNResult>>();
                            for (int year = DateTime.Now.Year; year > 2008; year--)
                            {
                                result = resultManager.GetModelStudent(year.ToString(), Request.QueryString["BH"].ToString());
                                if (result != null)
                                {
                                    result = resultManager.Translate(result);
                                    list.Add(TranslateResult(result));
                                    dic.Add(year.ToString(), list);
                                }
                            }
                            dics.Add(dic);
                            apiresult.studentresult = dics;
                            Response.Write(jser.Serialize(apiresult));
                            Response.End();
                        }
                    }
                }
            }
        }
        private static bool CheckIp(string ipAddr)
        {
            int num = Convert.ToInt32(ConfigurationManager.AppSettings["TotalGroups"].ToString());
            bool flag = false;
            for (int i = 1; i <= num; i++)
            {
                string ip = ConfigurationManager.AppSettings[string.Format("IP_Limit_Start_{0}", i)];
                string str2 = ConfigurationManager.AppSettings[string.Format("IP_Limit_End_{0}", i)];
                long num3 = getLongIp(ip);
                long num4 = getLongIp(str2);
                long num5 = getLongIp(ipAddr);
                if ((num5 >= num3) && (num5 <= num4))
                {
                    return true;
                }
                flag = false;
            }
            return flag;
        }
        private static long getLongIp(string ip)
        {
            string[] strArray = ip.Split(new char[] { '.' });
            long num = long.Parse(strArray[0]) << 0x18;
            long num2 = long.Parse(strArray[1]) << 0x10;
            long num3 = long.Parse(strArray[2]) << 8;
            long num4 = long.Parse(strArray[3]);
            return (((num + num2) + num3) + num4);
        }
        private string TranslateSchoolType(string stype)
        {
            switch(stype)
            {
                case "小学":
                    return "1";
                case "初中":
                    return "2";
                case "高中":
                    return "3";
                case "完中":
                    return "4";
                case "九年制":
                    return "5";
                default :
                    return "0";
            }
        }
        private string TranslateSex(string ssex)
        {
            if (ssex.Trim() == "男")
                return "1";
            else
                return "2";
        }
        private CNResult TranslateResult(HW_Result2013 result)
        {
            CNResult cnresult = new CNResult()
            {
                身高=result.SG.HasValue?Math.Round(result.SG.Value,1).ToString():"",
                身高评价=result.SGDJD,
                体重=result.TZ.HasValue?Math.Round(result.TZ.Value,1).ToString():"",
                体重评价=result.TZDJD,
                胸围=result.XW.HasValue?Math.Round(result.XW.Value,1).ToString():"",
                胸围评价=result.XWDJD,
                肺活量=result.FHL.HasValue?Math.Round(result.FHL.Value,1).ToString():"",
                肺活量评价=result.FHLDJD,
                腰围=result.YW.HasValue?Math.Round(result.YW.Value,1).ToString():"",
                臀围=result.TW.HasValue?Math.Round(result.TW.Value,1).ToString():"",
                收缩压=result.SSY.HasValue?Math.Round(result.SSY.Value,1).ToString():"",
                舒张压=result.SZY.HasValue?Math.Round(result.SZY.Value,1).ToString():"",
                血压 = (result.SZYPJ == "高血压"||result.SSYPJ=="高血压")?"高血压":"正常",
                脉搏=result.MB.HasValue?Math.Round(result.MB.Value,1).ToString():"",
                左眼视力=result.LSL.HasValue?Math.Round(result.LSL.Value,1).ToString():"",
                右眼视力=result.RSL.HasValue?Math.Round(result.RSL.Value,1).ToString():"",
                左眼屈光=result.LQG,
                右眼屈光=result.RQG,
                龋齿数=result.QSBNUM.HasValue?result.QSBNUM.Value.ToString():"",
                龋齿=result.QCPJ,
                牙周=result.YZB,
                沙眼=result.SYPJ,
                结膜炎=result.JMY,
                色觉=result.BS,
                听力=result.TL,
                耳病=result.EB,
                鼻病=result.BB,
                扁桃体=result.BTT,
                头部=result.TB,
                颈部=result.JB,
                胸部=result.XB,
                脊柱=result.JZ,
                四肢=result.SZ,
                皮肤=result.PF,
                淋巴结=result.LBJ,
                心脏=result.XZ,
                肺=result.FEI,
                肝=result.GP,
                脾=result.PEI,
                血红蛋白=result.XHDB.HasValue?result.XHDB.Value.ToString():"",
                结核菌素=result.JHJS,
                谷丙转氨酶=result.GBZAM.HasValue?Math.Round(result.GBZAM.Value,1).ToString():"",
                肝功=result.GG,
                表面抗原=result.BMKY,
                表面抗体=result.BMKT,
                粪蛔虫卵=result.FHCL,
                血型=result.BLOOD,
            };
            return cnresult;
        }
    }
    public class APISchool
    {
        public List<RowSchool> schoollist { get; set; }
    }
    public class RowSchool
    {
        public string SchoolBH{get;set;}
        public string SchoolName{get;set;}
        public string SchoolType{get;set;}
    }
    public class APIStudents
    {
        public List<APIStudent> studentinfo { get; set; }
    }
    public class APIResult
    {
        public List<Dictionary<string, List<CNResult>>> studentresult { get; set; }
    }
    public class CNResult
    {
        public string 身高 { get; set; }
        public string 身高评价 { get; set; }
        public string 体重 { get; set; }
        public string 体重评价 { get; set; }
        public string 胸围 { get; set; }
        public string 胸围评价 { get; set; }
        public string 肺活量 { get; set; }
        public string 肺活量评价 { get; set; }
        public string 腰围 { get; set; }
        public string 臀围 { get; set; }
        public string 收缩压 { get; set; }
        public string 舒张压 { get; set; }
        public string 血压 { get; set; }
        public string 脉搏 { get; set; }
        public string 左眼视力 { get; set; }
        public string 右眼视力 { get; set; }
        public string 左眼屈光 { get; set; }
        public string 右眼屈光 { get; set; }
        public string 龋齿数 { get; set; }
        public string 龋齿 { get; set; }
        public string 牙周 { get; set; }
        public string 沙眼 { get; set; }
        public string 结膜炎 { get; set; }
        public string 色觉 { get; set; }
        public string 听力 { get; set; }
        public string 耳病 { get; set; }
        public string 鼻病 { get; set; }
        public string 扁桃体 { get; set; }
        public string 头部 { get; set; }
        public string 颈部 { get; set; }
        public string 胸部 { get; set; }
        public string 脊柱 { get; set; }
        public string 四肢 { get; set; }
        public string 皮肤 { get; set; }
        public string 淋巴结 { get; set; }
        public string 心脏 { get; set; }
        public string 肺 { get; set; }
        public string 肝 { get; set; }
        public string 脾 { get; set; }
        public string 血红蛋白 { get; set; }
        public string 结核菌素 { get; set; }
        public string 谷丙转氨酶 { get; set; }
        public string 肝功 { get; set; }
        public string 表面抗原 { get; set; }

        public string 表面抗体 { get; set; }
        public string 粪蛔虫卵 { get; set; }
        public string 血型 { get; set; }
    }
}